package com.taofuxn.yycreazy.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.taofuxn.yycreazy.R;
import com.taofuxn.yycreazy.adpater.ZhuangbiAdapter;
import com.taofuxn.yycreazy.adpater.base.TaofuRecyclerAdapter;
import com.taofuxn.yycreazy.mvp.BasePresenter;
import com.taofuxn.yycreazy.mvp.MvpActivity;
import com.taofuxn.yycreazy.mvp.model.ZhuangbiBean;
import com.taofuxn.yycreazy.mvp.presenter.ZhuangbiPresenter;
import com.taofuxn.yycreazy.mvp.view.IZhuangbiView;
import com.taofuxn.yycreazy.util.MyLog;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 装逼宝典
 * Created by taofuxn on 2016/9/21.
 */
public class ZhuangbiActivity extends MvpActivity implements IZhuangbiView, SwipeRefreshLayout.OnRefreshListener {
    @InjectView(R.id.recyclerview)
    RecyclerView recyclerview;
    @InjectView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private ZhuangbiPresenter mZhuangbiPresenter;

    private ZhuangbiAdapter mZhuangbiAdapter;
    private ArrayList<ZhuangbiBean> mDatas;
    /**
     * 列数
     */
    private final int colnmNum = 2;

    private String key = "";

    @Override
    protected BasePresenter createDefaultPresenter() {
        mZhuangbiPresenter = new ZhuangbiPresenter(this);
        aotuReflash();
        return mZhuangbiPresenter;
    }

    @Override
    protected int setContentLayout() {
        return R.layout.activity_zhuangbi;
    }

    @Override
    protected void init() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        toolbar .setNavigationIcon(R.mipmap.abc_ic_ab_back_mtrl_am_alpha);
        toolbar .setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mDatas = new ArrayList<>();
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeColors(Color.BLACK,Color.RED,Color.BLUE,Color.GREEN);

        StaggeredGridLayoutManager sgm = new StaggeredGridLayoutManager(colnmNum,StaggeredGridLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(sgm);
        mZhuangbiAdapter = new ZhuangbiAdapter(R.layout.item_zhuangbi_list,mDatas);
        recyclerview.setAdapter(mZhuangbiAdapter);
    }

    public void aotuReflash(){
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
            }
        });
        onRefresh();
    }


    @Override
    public void requestSuccess(ArrayList<ZhuangbiBean> been) {
            mDatas.clear();
            mDatas.addAll(been);
            MyLog.showLog(key);
            mZhuangbiAdapter.notifyDataSetChanged();
    }

    @Override
    public void dismissLoading() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        mZhuangbiPresenter.getZhuangbiList(key);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_zhuangbi_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id){
            case R.id.search_badge:
                forward(SearchActivity.class,0x001);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0x001){
            key = data.getStringExtra("key");
            aotuReflash();
        }
    }

}
